package com.jxs.android.ges;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.jxs.android.ges.data.A_Case_Cst_RelationInfo;
import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.soap.SoapUtil;
import com.jxs.android.util.HandlerHolder;

public class A_Case_GetRelationBaseInfoHolder {

	private Handler handler;
	private int pageIndex = -1;

	public A_Case_GetRelationBaseInfoHolder(Handler handler, int pageIndex) {
		this.handler = handler;
		this.pageIndex = pageIndex;
	}

	public void getContactList(final String ticket, final Object... params) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				String wsdl_url = GesApplication.WSDL_URL;// wsdl
				String namespace = GesApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "getContactList";//
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("sys_no", params[0].toString());
				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						SoapObject response = (SoapObject) envelope.getResponse();
						paser_new_Interface(response, params);
					}

					@Override
					public void error(String message, Exception e) {
						Message msg = new Message();
						msg.what = HandlerHolder.What.ERROR_MSG;
						msg.obj = message;
						handler.sendMessage(msg);
					}

				});
			}
		}).start();
	}

	/**
	 * @Des this method will be used to when update interface protocol
	 * @param soapObject
	 * @param params
	 */
	private void paser_new_Interface(SoapObject soapObject, Object... params) {
		if (null != soapObject) {
			String stauts = soapObject.getProperty("status").toString();
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getProperty("message").toString();
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = HandlerHolder.What.TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				SoapObject taskBeanObject = (SoapObject) soapObject.getProperty("retMsg");
				Message msg = new Message();
				msg.what = HandlerHolder.What.UPDATE_LIST_UI;
				msg.setData(new Bundle());
				msg.arg1 = pageIndex;
				msg.obj = paserData(taskBeanObject, params);
				handler.sendMessage(msg);
			}
		}
	}

	private Object paserData(SoapObject retMsgObject, Object... params) {
		ArrayList<A_Case_Cst_RelationInfo> tems = new ArrayList<A_Case_Cst_RelationInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				A_Case_Cst_RelationInfo cstInfo = new A_Case_Cst_RelationInfo();
				cstInfo.birthday = SoapUtil.getValue(taskBeanObject, "birthday");
				cstInfo.name = SoapUtil.getValue(taskBeanObject, "name");
				cstInfo.phone = SoapUtil.getValue(taskBeanObject, "phone");
				cstInfo.type_name = SoapUtil.getValue(taskBeanObject, "type_name");
				tems.add(cstInfo);
			}
		}
		return tems;
	}
}
